﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using NHibernateToSQLMapping.Entity;

namespace NHibernateToSQLMapping.DAL
{
    public class SQLServer2008DAL : SQLServerDAL
    {
        /// <summary>
        /// 查询所有字段属性
        /// </summary>
        /// <param name="conStr">连接字符串</param>
        /// <returns></returns>
        public override DataSet GetTableInfo(string conStr, string tableName)
        {
            string sql = @"select TableName=a.name,[Description]=f.value from sysobjects as a
left join sys.extended_properties as f on a.id=f.major_id and f.minor_id=0
where a.name=@objname
            SELECT b.name AS ColumnName
            , c.name AS DataType
            , b.length AS [Length]
            , e.indid AS IsPrimarykey
            , f.value AS [Description]
            , b.status AS [Status]
            , b.isnullable AS AllowNulls
            , a.name AS TableName
            , a.id AS TableID
            ,IsIdentity=COLUMNPROPERTY(b.id,b.name,'IsIdentity')
            FROM sysobjects as a
            LEFT JOIN syscolumns as b ON a.id=b.id
            LEFT JOIN systypes as c ON b.xusertype=c.xusertype
            LEFT JOIN sysindexkeys as e ON b.id=e.id AND b.colid=e.colid AND e.indid=1
            LEFT JOIN sys.extended_properties as f ON b.id=f.major_id AND b.colid=f.minor_id
            where a.name=@objname order by e.indid desc";
            SqlParameter[] values = {
                                    new SqlParameter("@objname",tableName)};
            List<string> lst = new List<string>();
            DataSet ds = SqlHelper.ExecuteGetDataSet(conStr, System.Data.CommandType.Text, sql, values);
            return ds;
        }
    }
}
